package com.ycdr.leetcode.queue.example;

/**
 * 基于数组实现的Queue
 * @date 2020年06月01日
 * @author ycdr
 */
public class ArrayQueue {
    private int maxSize;
    private int rear;
    private int front;
    private int noItmes;
    private Object[] queue;

    public ArrayQueue(int i){
        this.maxSize=i;
        this.rear=-1;
        this.front=-1;
        this.noItmes=0;
        this.queue=new Object[i];
    }

    public boolean insert(Object o){
        if (isFull()){
            System.out.println("Queue is Full");
            return false;
        }
        this.queue[++front]=o;
        noItmes++;
        return true;
    }

    public Object remove(){
        if (isEmpty()){
            System.out.println("Queue is Empty");
            return null;
        }
        noItmes--;
        return this.queue[++rear];
    }

    public Object peek(){
        if (isEmpty()){
            System.out.println("Queue is Empty");
            return null;
        }
        int t=rear;
        return this.queue[++t];
    }

    public boolean isFull(){
        int t=front;
        return maxSize==++t;
    }

    public boolean isEmpty(){
        return noItmes==0;
    }

    public static void main(String[] args) {
        ArrayQueue queue=new ArrayQueue(3);
        System.out.println(queue.isEmpty());
        System.out.println(queue.isFull());
        queue.insert(1);
        queue.insert(2);
        queue.insert(3);
        System.out.println(queue.isEmpty());
        System.out.println(queue.isFull());


        System.out.println(queue.peek());

        System.out.println(queue.remove());
        System.out.println(queue.remove());
        System.out.println(queue.remove());

        System.out.println(queue.peek());

    }
}
